//package com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.method;
//
//import com.inspur.edp.bef.bizentity.GspBizEntityObject;
//import com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.BefRtEntityGenCtx;
//import com.inspur.edp.bef.spi.entity.CodeRuleAssembler;
//import com.inspur.edp.bef.spi.entity.CodeRuleConfig;
//import com.inspur.edp.bef.spi.entity.CodeRuleInfo;
//import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
//import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
//import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
//import com.inspur.edp.caf.generator.utils.ExpressionUtils;
//import com.inspur.edp.cef.api.attr.CodeRuleInfoAttribute;
//import com.inspur.edp.cef.gencommon.ReflectionUtils;
//import com.inspur.edp.cef.gencommon.config.CefConfigUtils;
//import com.inspur.edp.cef.rtgenerator.common.CefEntityTypeInfo;
//import lombok.var;
//import org.eclipse.jdt.core.dom.Block;
//import org.eclipse.jdt.core.dom.Modifier;
//import org.eclipse.jdt.core.dom.ReturnStatement;
//import org.eclipse.jdt.core.dom.Statement;
//
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.HashMap;
//
//public class GetCodeRuleAssemblerGenerator extends ClassMethodGenerator {
//    private BefRtEntityGenCtx context;
//    private GspBizEntityObject node;
//
//    private static final String ParamAssembler = "codeRuleAssembler";
//    private static final String ParamB4SaveDic = "b4SaveDic";
//    private static final String ParamAfterCreateDic = "afterCreateDic";
//
//    public GetCodeRuleAssemblerGenerator(BefRtEntityGenCtx context,  GspBizEntityObject node) {
//        this.context = context;
//        this.node = node;
//    }
//
//    @Override
//    protected Block buildMethodBody() {
//        var block = ast.newBlock();
//        block.statements().add(createInstanceStatement());
//
//        getBeforeSave(block);
//        getAfterCreate(block);
//        addImport(CodeRuleInfo.class.getTypeName());
//        block.statements().add(getReturnStatement());
//        return block;
//    }
//
//    private Statement createInstanceStatement(){
//        var frag = ast.newVariableDeclarationFragment();
//        frag.setName(ast.newSimpleName(ParamAssembler));
//        var creation = ast.newClassInstanceCreation();
//        creation.setType(new TypeInfo(CodeRuleAssembler.class).getType(ast));
//        frag.setInitializer(creation);
//        var statement = ast.newVariableDeclarationStatement(frag);
//        statement.setType(new TypeInfo(CodeRuleAssembler.class).getType(ast));
//        return statement;
//    }
//
//    private ReturnStatement getReturnStatement(){
//        var returnStatement = ast.newReturnStatement();
//        returnStatement.setExpression(ast.newSimpleName(ParamAssembler));
//        return returnStatement;
//    }
//    @Override
//    protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
//        return new ArrayList<Modifier.ModifierKeyword>(Arrays.asList(new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PROTECTED_KEYWORD}));
//    }
//
//    @Override
//    protected boolean getIsOverride() {
//        return true;
//    }
//
//    @Override
//    protected String getMethodName() {
//        return "getCodeRuleAssembler";
//    }
//
//    @Override
//    protected ArrayList<ParameterInfo> getParameterCollection() {
//        return new ArrayList<ParameterInfo>();
//    }
//
//    @Override
//    protected TypeInfo getReturnType() {
//        return new TypeInfo(CodeRuleAssembler.class);
//    }
//
//    private HashMap<String, CodeRuleInfo> afterCreateResult() {
//        HashMap<String, CodeRuleInfo> result = null;
//        var entityType = entityTypeInfo.getActionImplType();
//
//        var attribute = entityType.getAnnotation(CodeRuleInfoAttribute.class);
//        CodeRuleInfoAttribute attr = (CodeRuleInfoAttribute) ((attribute instanceof CodeRuleInfoAttribute) ? attribute : null);
//        if (attr != null) {
//            Class realType =getType(attr);
//
//            Object tempVar = ReflectionUtils.createInstance(realType);
//            CodeRuleConfig config = (CodeRuleConfig) ((tempVar instanceof CodeRuleConfig) ? tempVar : null);
//            if (config != null)
//                result = config.afterCreate();
//        }
//        if (result == null) {
//            result = new HashMap<String, CodeRuleInfo>();
//        }
//
//
//        var extend = context.befGenCtx.getMgrTypes();
//
//        for (var item : extend) {
//
//            var extendEntityType = item.getEntityEntityTypeInfo().getActionImplType();
//
//            var extendAttribute = extendEntityType.getAnnotation(CodeRuleInfoAttribute.class);
//            CodeRuleInfoAttribute extendAttr = (CodeRuleInfoAttribute) ((attribute instanceof CodeRuleInfoAttribute) ? attribute : null);
//            if (extendAttr != null) {
//
//                Class realType =getType(extendAttr);
//                Object tempVar2 =ReflectionUtils.createInstance(realType);//Activator.CreateInstance(extendAttr.Type);
//                CodeRuleConfig config = (CodeRuleConfig) ((tempVar2 instanceof CodeRuleConfig) ? tempVar2 : null);
//
//                var afterCreate = config.afterCreate();
//                if (afterCreate == null || afterCreate.size() == 0) {
//                    continue;
//                } else {
//
//                    for (var key : afterCreate.keySet()) {
//                        result.put(key, afterCreate.get(key));
////                        if (result.containsKey(key)) {
////                            result.put(key, afterCreate.get(key));
////                        } else {
////                            result.put(key, afterCreate.get(key));
////                        }
//                    }
//                }
//            }
//
//        }
//
//        return result;
//    }
//
//
//    private Class getType(CodeRuleInfoAttribute attr){
//        Class realType ;
//        var compileContext = context.befGenCtx.getCompileContext();
//        if (compileContext == null) {
//            realType = CefConfigUtils.getType(null, attr.type().getName());
//        } else {
//            ArrayList<String> jarList = new ArrayList<String>() {
//                {
//                    add(compileContext.getApiJarFileName());
//                    add(compileContext.getCoreJarFileName());
//                }
//            };
//            realType = CefConfigUtils.getType(jarList, null, attr.type().getName());
//        }
//        if (realType == null)
//            realType = attr.type();
//
//        return realType;
//    }
//
//    private HashMap<String, CodeRuleInfo> beforeSaveResult() {
//        HashMap<String, CodeRuleInfo> result = null;
//
//        var basic = context.befGenCtx.getCurrentTypeInfo();
//        var entityType = entityTypeInfo.getActionImplType();
//
//        var attribute = entityType.getAnnotation(CodeRuleInfoAttribute.class);
//        CodeRuleInfoAttribute attr = (CodeRuleInfoAttribute) ((attribute instanceof CodeRuleInfoAttribute) ? attribute : null);
//        if (attr != null) {
//            var realType = getType(attr);
//            Object tempVar = ReflectionUtils.createInstance(realType);
//            CodeRuleConfig config = (CodeRuleConfig) ((tempVar instanceof CodeRuleConfig) ? tempVar : null);
//            result = config.beforeSave();
//        }
//        if (result == null) {
//            result = new HashMap<String, CodeRuleInfo>();
//        }
//
//
//        var extend = context.befGenCtx.getMgrTypes();
//
//        for (var item : extend) {
//
//            var extendEntityType = item.getEntityEntityTypeInfo().getActionImplType();
//
//            var extendAttribute = extendEntityType.getAnnotation(CodeRuleInfoAttribute.class);
//            CodeRuleInfoAttribute extendAttr = (CodeRuleInfoAttribute) ((attribute instanceof CodeRuleInfoAttribute) ? attribute : null);
//            if (attr != null) {
//                var realType = getType(extendAttr);
//                Object tempVar2 = ReflectionUtils.createInstance(realType);
//                CodeRuleConfig config = (CodeRuleConfig) ((tempVar2 instanceof CodeRuleConfig) ? tempVar2 : null);
//
//                var beforeSave = config.beforeSave();
//                if (beforeSave == null || beforeSave.size() == 0) {
//                    continue;
//                } else {
//
//                    for (var key : beforeSave.keySet()) {
//                        result.put(key, beforeSave.get(key));
////                        if (result.containsKey(key)) {
////                            result.put(key, beforeSave.get(key));
////                        } else {
////                            result.put(key, beforeSave.get(key));
////                        }
//                    }
//                }
//            }
//
//        }
//
//        return result;
//    }
//
//
//    private void getBeforeSave(Block block) {
//       block.statements().add(getCreateDicStatement(ParamB4SaveDic));
//
//        getBeforeSaveContent(block);
//
//        block.statements().add(setValStatement("setBeforeSave", ParamB4SaveDic));
//
//    }
//
//    private Statement setValStatement(String methodName,String valName){
//        var invocation = ExpressionUtils.getMethodInvocation(ast, ParamAssembler, methodName);
//        invocation.arguments().add(ast.newSimpleName(valName));
//        return ast.newExpressionStatement(invocation);
//    }
//    private TypeInfo getRuleInfoDicType(){
//        var typeInfo = new TypeInfo(HashMap.class);
//        typeInfo.setIsParameterizedType(true);
//        typeInfo.getArguments().add(new TypeInfo(String.class));
//        typeInfo.getArguments().add(new TypeInfo(CodeRuleInfo.class));
//        return typeInfo;
//    }
//
//    private Statement getCreateDicStatement(String varName){
//        var frag = ast.newVariableDeclarationFragment();
//        frag.setName(ast.newSimpleName(varName));
//        var creation = ast.newClassInstanceCreation();
//        creation.setType(getRuleInfoDicType().getType(ast));
//        frag.setInitializer(creation);
//        var statement = ast.newVariableDeclarationStatement(frag);
//        statement.setType(getRuleInfoDicType().getType(ast));
//        return statement;
//    }
//    private void getAfterCreate(Block block) {
//        block.statements().add(getCreateDicStatement(ParamAfterCreateDic));
//
//        getAfterCreateContent(block);
//
//        block.statements().add(setValStatement("setAfterCreate", ParamAfterCreateDic));
//    }
//
//    private void getBeforeSaveContent(Block block) {
//        HashMap<String, CodeRuleInfo> dic = beforeSaveResult();
//
//        for (var key : dic.keySet()) {
//
//            String varName = key+"B4Info";
//            block.statements().add(getCreateInfoStatement(varName));
//            block.statements().add(getSetRuleIdStatement(varName, dic.get(key).getCodeRuleId()));
//            block.statements().add(getSetDicStatement(ParamB4SaveDic, key, varName));
//        }
//
//    }
//
//    private Statement getCreateInfoStatement(String varName){
//        var frag = ast.newVariableDeclarationFragment();
//        frag.setName(ast.newSimpleName(varName));
//        var creation = ast.newClassInstanceCreation();
//        creation.setType(new TypeInfo(CodeRuleInfo.class).getType(ast));
//        frag.setInitializer(creation);
//        var statement = ast.newVariableDeclarationStatement(frag);
//        statement.setType(new TypeInfo(CodeRuleInfo.class).getType(ast));
//        return statement;
//    }
//
//    private Statement getSetRuleIdStatement(String varName, String ruleId){
//        var invocation = ExpressionUtils.getMethodInvocation(ast, varName, "setCodeRuleId");
//        invocation.arguments().add(ExpressionUtils.getStringLiteral(ast, ruleId));
//        return ast.newExpressionStatement(invocation);
//    }
//
//    private Statement getSetDicStatement(String dicParamName, String key, String ruleParamName){
//        var invocation = ExpressionUtils.getMethodInvocation(ast, dicParamName, "put");
//        invocation.arguments().add(ExpressionUtils.getStringLiteral(ast, key));
//        invocation.arguments().add(ast.newSimpleName(ruleParamName));
//        return ast.newExpressionStatement(invocation);
//    }
//
//    private void getAfterCreateContent(Block block) {
//        HashMap<String, CodeRuleInfo> dic = afterCreateResult();
//
//        for (var key : dic.keySet()) {
//
//            String varName = key+"AcInfo";
//            block.statements().add(getCreateInfoStatement(varName));
//            block.statements().add(getSetRuleIdStatement(varName, dic.get(key).getCodeRuleId()));
//            block.statements().add(getSetDicStatement(ParamAfterCreateDic, key, varName));
//        }
//
//    }
//}